import BaseAds from "./BaseAds";
import { WeChatAd } from "./WeChatAd";
import { EventTool } from "../Tool/EventTool";
import KongFuLoader from "../../MyLoader/KongFuResourLoader";
import { QQAds } from "./QQAds";


export enum PlatformType {
    FaceBook = "FaceBook",
    WeChat = "WeChat",
    QQ = "OnMQQBrowser",
    Oppo = "OnOppo",
    Vivo = "Vivo",
    Titalk = "Titalk",
    None = "None",
}


export default class GameAdsManager {
    private static m_instance: GameAdsManager = null;
    private m_ads: BaseAds = null;
    private m_useshare: boolean = false;


    public static get Instance()  {
        if (GameAdsManager.m_instance == null) {
            GameAdsManager.m_instance = new GameAdsManager();
        }
        return GameAdsManager.m_instance;
    }

    private m_hasAds = true;

    //运行环境======
    private s_platformType: PlatformType = PlatformType.None;

    public get Platform(): PlatformType  {
        let isQTT = window["qttGame"] != null
        let isTT = window["tt"] != null
        let isQQ = window["qq"] != null
        if (isQQ || Laya.Browser.onMQQBrowser)
            this.s_platformType = PlatformType.QQ;
        else if (isTT)
            this.s_platformType = PlatformType.Titalk;
        else if (typeof FBInstant !== 'undefined')
            this.s_platformType = PlatformType.FaceBook;
        else if (Laya.Browser.onWeiXin)
            this.s_platformType = PlatformType.WeChat;
        else if (Laya.Browser.onQGMiniGame)
            this.s_platformType = PlatformType.Oppo;
        else if (Laya.Browser.onVVMiniGame)
            this.s_platformType = PlatformType.Vivo;
        return this.s_platformType;
    }

    
    private s_miniPlatform: any;
    public get MiniPlatform(){
        switch (this.Platform) {
            case PlatformType.WeChat:
                this.s_miniPlatform = wx;
                break;
            case PlatformType.QQ:
                this.s_miniPlatform = qq;
                break;
            case PlatformType.Oppo:
                this.s_miniPlatform = qg;
                break;
            default:
                break;
        }
        return this.s_miniPlatform;
    }


    constructor()  {
        this.InitBaseAds();
    }


    private InitBaseAds()  {

        let platformType = this.Platform;
        switch (platformType) {
            case PlatformType.WeChat:
                this.m_ads = new WeChatAd(this.MiniPlatform);
                break;
            case PlatformType.QQ:
                this.m_ads = new QQAds(this.MiniPlatform);
                break;
            default:
                console.warn("未找到广告平台类型：" + platformType);
                this.m_hasAds = false;
                break;
        }
    }

    public SetUseShare(useshare: boolean)  {
        this.m_useshare = useshare;
    }


    public InitAd(ishowbannerfc: Function = null, ishowintersitialfc: Function = null)  {
        if (!this.m_hasAds) {
            return;
        }
        this.m_ads.InitAd(ishowbannerfc, ishowintersitialfc);
    }

    public get AdsSystemInfo()  {
        if (this.m_ads != null) {
            return this.m_ads.SyncSystemInfo;
        }

        return null;
    }


    public ShowBannerAd(bannertype: string)  {
        if (!this.m_hasAds) {
            return;
        }
        this.m_ads.ShowBannerAd(bannertype);
    }

    public HideBannerAd(bannertype: string)  {
        if (!this.m_hasAds) {
            return;
        }
        this.m_ads.HideBannerAd(bannertype);
    }

    public ShowRewardVieoAd(callback: Function)  {
        if (this.m_useshare) {
            EventTool.ShareWithFriend(callback);
        }
        else  {
            if (this.Platform == PlatformType.None || !this.m_hasAds) {
                callback(true);
                return;
            }
            this.m_ads.ShowRewardVieoAd(callback);
        }
    }

    public ShowInterstitialAd()  {
        if (!this.m_hasAds) {
            return;
        }
        this.m_ads.ShowInterstitialAd();
    }

    public NativeToProgram(gameId: string, gameName, path: string, adtype: string, order: number)  {
        if (!this.m_hasAds) {
            return;
        }
        this.m_ads.NativeToProgram(gameId, gameName, path, adtype, order);
    }

    public CanGetMenueButtonOrLoadSubPackage()  {
        if (!this.m_hasAds) {
            return false;
        }
        return this.m_ads.CanGetMenueButtonOrLoadSubPackage();
    }


    private m_topMenueRect: _MenueButtonBoundingInfo = null;
    public GetMenueBtnTop(): _MenueButtonBoundingInfo  {
        if (this.m_topMenueRect != null) {
            return this.m_topMenueRect;
        }
        if (this.AdsSystemInfo == null) {
            this.m_topMenueRect = {  //获取不到胶囊信息就自定义重置一个
                bottom: 32,
                height: 32,
                left: Laya.Browser.clientWidth - 98,
                right: Laya.Browser.clientWidth - 10,
                top: 32,
                width: 88
            };
            return this.m_topMenueRect;
        }

        let getpaltformsuccess: boolean = true;
        if (this.MiniPlatform == null || this.MiniPlatform == "undefined") {
            getpaltformsuccess = false;
        }
        let rect = this.MiniPlatform.getMenuButtonBoundingClientRect ? this.MiniPlatform.getMenuButtonBoundingClientRect() : null;
        if (rect === null) {
            getpaltformsuccess = false;
        }
        //取值为0的情况
        if (!rect.width) {
            getpaltformsuccess = false;
        }
        if (getpaltformsuccess) {
            let gap = rect.top - this.AdsSystemInfo.statusBarHeight; //动态计算每台手机状态栏到胶囊按钮间距
            let width = rect.width;
            let height = 2 * gap + rect.height
            this.m_topMenueRect =
                {
                    //获取不到胶囊信息就自定义重置一个
                    bottom: this.AdsSystemInfo.statusBarHeight + height,
                    height: height,
                    left: rect.left,
                    right: rect.right,
                    top: rect.top,
                    width: width
                };
        } else {
            let gap = 0; //胶囊按钮上下间距 使导航内容居中
            let width = 96; //胶囊的宽度，android大部分96，ios为88
            if (this.AdsSystemInfo.platform === 'android') {
                gap = 8;
                width = 96;
            }
            else if (this.AdsSystemInfo.platform === 'devtools') {
                if (this.AdsSystemInfo.platform === 'ios') {
                    gap = 5.5; //开发工具中ios手机
                } else {
                    gap = 7.5; //开发工具中android和其他手机
                }
            } else {
                gap = 4;
                width = 88;
            }
            if (!this.AdsSystemInfo.statusBarHeight) {
                //开启wifi的情况下修复statusBarHeight值获取不到
                this.AdsSystemInfo.statusBarHeight = this.AdsSystemInfo.screenHeight - this.AdsSystemInfo.windowHeight - 20;
            }
            this.m_topMenueRect = {
                //获取不到胶囊信息就自定义重置一个
                bottom: this.AdsSystemInfo.statusBarHeight + gap + 32,
                height: 32,
                left: this.AdsSystemInfo.windowWidth - width - 10,
                right: this.AdsSystemInfo.windowWidth - 10,
                top: this.AdsSystemInfo.statusBarHeight + gap,
                width: width
            };
        }
        return this.m_topMenueRect;
    }


    /*** 是否已经存在激励视频*/
    public HasRewardVideo()  {
        if (!this.m_hasAds) {
            return false;
        }
        return this.m_ads.HasRewardVideo;
    }

    /**
    * 是否已经存在插评广告
    */
    public get HasInterstiatialAd()  {
        if (!this.m_hasAds) {
            return false;
        }
        return this.m_ads.HasInterstiatialAd;
    }

    OnHide(callback: Function)  {
        if (this.MiniPlatform == null || this.MiniPlatform == "undefined")
            return;

        this.MiniPlatform.onHide(callback);
    }

    OnShow(callback: Function)  {
        if (this.MiniPlatform == null || this.MiniPlatform == "undefined")
            return;
        this.MiniPlatform.onShow(callback);
    }

    OnAudioInterruptionEnd(callback: Function)  {
        console.log("OnAudioInterruptionEnd");
        if (!this.m_hasAds) {
            return;
        }
        console.log("OnAudioInterruptionEnd");
        this.m_ads.OnAudioInterruptionEnd(callback);
    }


    //========================================================OnlyQQ===============================================
    /**显示盒子广告 */
    ShowAppBoxAd()  {
        if (!this.m_hasAds) {
            return;
        }
        this.m_ads.ShowAppBoxAd();
    }
    /**关闭盒子广告 */
    HideAppBoxAd() {
        if (!this.m_hasAds) {
            return;
        }
        this.m_ads.HideAppBoxAd();
    }

    /**显示积木广告 */
    ShowBlockAd(blockAdType: string)  {
        if (!this.m_hasAds) {
            return;
        }
        this.m_ads.ShowBlockAd(blockAdType);
    }
    /**隐藏积木广告 */
    HideBlockAd(blockAdType: string) {
        if (!this.m_hasAds) {
            return;
        }
        this.m_ads.HideBlockAd(blockAdType);
    }

    //===================================================================================
}